import {defineStore} from "pinia";
import {ref} from "vue";

export const useCountStore = defineStore('count', () => {
        const waitCode = ref(false) // 用户验证码获取次数
        const timer = ref()// 定时器
        const timeRemaining = ref(60)// 倒计时时间
        const start = () => {
            clearInterval(timer.value)
            // 开启定时器
            timer.value = setInterval(() => {
                timeRemaining.value--
                if (timeRemaining.value <= 0) {
                    clearInterval(timer.value)
                    timeRemaining.value = 60
                    timer.value = undefined
                    waitCode.value = false
                }
            }, 1000)
            // return uni.showToast({title: '获取验证码过于频繁，请稍后再试', icon: 'none'})
        }
        return {
            waitCode,
            timer,
            timeRemaining,
            start
        }
    },
    {
        persist: {
            storage: {
                setItem(key, value) {
                    uni.setStorageSync(key, value)
                },
                getItem(key) {
                    return uni.getStorageSync(key)
                },
            },
        },
    }
)